﻿using Common.Attributes;
using MODEL;
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace QGZX.Logic.Zmjh
{
    public class PeriodController :Controller
    {
        public ActionResult index()
        {
            return View();
        }


        [HttpPost]
        [AjaxRequest]
        /// <summary>
        /// 获取竹铭课程期数数据列表
        /// </summary>
        /// <retur
        public ActionResult GetPeriodData()
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);

            var list = OperateContext.Current.BLLSession.Iqgzx_zm_periodBLL.GetPagedList(pageIndex, pageSize, x => x.Id != null, m => m.Id).Select(x => x.ToPOCO());
            int rowCount = OperateContext.Current.BLLSession.Iqgzx_zm_periodBLL.GetListBy(x => x.Id != null).Count();

            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }

        #region 加载修改竹铭课程 window 的html + EditPeriod()
        /// <summary>
        /// 加载修改竹铭期数开启 window 的html
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult EditPeriod(int id)
        {

            //根据id 查询要修改的权限
            var model = OperateContext.Current.BLLSession.Iqgzx_zm_periodBLL.GetListBy(x => x.Id == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post修改课程信息 EditPeriod(MODEL.qgzx_zm_course model)
        /// <summary>
        ///post修改期数信息 EditPeriod(MODEL.qgzx_zm_period model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult EditPeriod(MODEL.qgzx_zm_period model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_zm_periodBLL.Modify(model, "Id", "Period", "IsOpen", "BeginTime", "EndTime");
            if (res > 0)
            {
                return Redirect("/Zmjh/Period/index");
            }
            return Redirect("/Zmjh/Period/index");
        }
        #endregion

        #region 在这新增课程 html代码 + AddPeriod()
        /// <summary>
        /// 在这新增新一期竹铭并且开启报名系统  html代码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AddPeriod()
        {

            //SetDropDonwList();
            return PartialView("EditPeriod");
        }
        #endregion

        #region 在这新增课程 html代码 + AddPeriod(MODEL.qgzx_zm_period model)
        /// <summary>
        /// 在这新增期数 html代码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult AddPeriod(MODEL.qgzx_zm_period model)
        {

            int res = OperateContext.Current.BLLSession.Iqgzx_zm_periodBLL.Add(model);

            if (res > 0)
            {
                return Redirect("/Zmjh/Period/index?ok");
            }
            return Redirect("/Zmjh/Period/index?err");
        }
        #endregion

        #region 删除课程 + DelPeriod()
        /// <summary>
        /// 删除课程 + DelCourse()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult DelPeriod()
        {
            try
            {
                int id = int.Parse(Request.Form["id"]);
                OperateContext.Current.BLLSession.Iqgzx_zm_periodBLL.DelBy(x => x.Id == id);
                return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
            }
            catch (Exception ex)
            {

                return OperateContext.Current.RedirectAjax("err", "您现在删除的权限正在被使用,请先取消角色中的这个权限" + ex, null, "");
            }
        }
        #endregion



        //-------------------------------课程详细信息---------------------------------

        #region index详细课程信息视图----
        /// <summary>
        /// index视图
        /// </summary>
        /// <returns></returns>
        public ActionResult CourseDetail()
        {
            return PartialView();
        } 
        #endregion

        #region 获取每一期课程的详细信息+GetCourseDetailData（）
        /// <summary>
        /// 获取每一期课程的详细信息+GetCourseDetailData（）
        /// </summary>
        /// <returns></returns>
        public ActionResult GetCourseDetailData()
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);

            var list = OperateContext.Current.BLLSession.Iqgzx_zm_course_situationBLL.GetPagedList(pageIndex, pageSize, x => x.Id != null, m => m.Id);
            int rowCount = OperateContext.Current.BLLSession.Iqgzx_zm_course_situationBLL.GetListBy(x => x.Id != null).Count();

            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 根据第几期的id来查询当期的课程详细资料  +  CurrentCourse()
        /// <summary>
        /// 根据第几期的id来查询当期的课程详细资料
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult CurrentCourse(int id)
        {

            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            qgzx_zm_period model = OperateContext.Current.BLLSession.Iqgzx_zm_periodBLL.GetListBy(x => x.Id == id, m => m.Id).FirstOrDefault();
            var reslist = OperateContext.Current.BLLSession.Iqgzx_zm_course_situationBLL.GetPagedList(pageIndex, pageSize, x => x.Period == model.Period, m => m.Id).Select(x => x.ToPOCO());
            int rowCount = OperateContext.Current.BLLSession.Iqgzx_zm_course_situationBLL.GetListBy(x => x.Id != null && x.Period == model.Period).Count();
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = reslist,
                footer = null
            };
            return Json(dgMode);
        } 
        #endregion

        #region 加载修改竹铭课程 window 的html + EditCourseSituation()
        /// <summary>
        /// 加载修改竹铭课程 window 的html
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult EditCourseSituation(int id)
        {

            //根据id 查询要修改的权限
            SetDropDonwList();
            var model = OperateContext.Current.BLLSession.Iqgzx_zm_course_situationBLL.GetListBy(x => x.Id == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post修改课程信息 EditCourseSituation(MODEL.qgzx_zm_course model)
        /// <summary>
        ///post修改课程详细情况 EditCourseSituation(MODEL.qgzx_zm_course_situation model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult EditCourseSituation(MODEL.qgzx_zm_course_situation model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_zm_course_situationBLL.Modify(model, "Id", "Period", "CourseNum", "CourseId", "Teacher", "TeacherIntroduce",
                "CoursePlan", "ClassTime", "ClassLocation", "ClassNum", "ToBegin", "ToEnd", "EnrollNum");
            if (res > 0)
            {
                return Redirect("/Zmjh/CourseSituation/index");
            }
            return Redirect("/Zmjh/CourseSituation/index");
        }
        #endregion

        #region 在这新增课程 html代码 + AddCourseSituation()
        /// <summary>
        /// 在这新增课程 html代码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AddCourseSituation()
        {

            SetDropDonwList();
            return PartialView("EditCourseSituation");
        }
        #endregion

        #region 在这新增课程 html代码 + AddCourseSituation(MODEL.qgzx_zm_course_situation model)
        /// <summary>
        /// 在这新增课程 html代码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult AddCourseSituation(MODEL.qgzx_zm_course_situation model)
        {

            int res = OperateContext.Current.BLLSession.Iqgzx_zm_course_situationBLL.Add(model);

            if (res > 0)
            {
                return Redirect("/Zmjh/CourseSituation/index?ok");
            }
            return Redirect("/Zmjh/CourseSituation/index?err");
        }
        #endregion

        #region 删除课程 + DelCourseSituation()
        /// <summary>
        /// 删除课程 + DelCourse()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult DelCourseSituation()
        {
            try
            {
                int id = int.Parse(Request.Form["id"]);
                OperateContext.Current.BLLSession.Iqgzx_zm_course_situationBLL.DelBy(x => x.Id == id);
                return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
            }
            catch (Exception ex)
            {

                return OperateContext.Current.RedirectAjax("err", "您现在删除的权限正在被使用,请先取消角色中的这个权限" + ex, null, "");
            }
        }
        #endregion

        #region 高级搜索(比较笨，先用这个方法) + ActionResult Search()
        /// <summary>
        /// 高级搜索(比较笨，先用这个方法)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Search()
        {
            string Period = Request.QueryString["Period"];
            string CourseNum = Request.QueryString["CourseNum"];

            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //高级搜索的功能！！！
            var list = OperateContext.Current.BLLSession.Iqgzx_zm_course_situationBLL.GetListBy(x => x.Id != null).ToList();
            if (Period != "")
            {
                list = list.Where(x => x.Period.ToString().Contains(Period)).ToList();
            }
            if (CourseNum != "")
            {
                list = list.Where(x => x.CourseNum.Contains(CourseNum)).ToList();
            }

            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 设置新增和修改通用的下拉框数据 + SetDropDonwList()
        /// <summary>
        /// 设置新增和修改通用的下拉框数据
        /// </summary>
        void SetDropDonwList()
        {
            /*
             课程id对应课程
             */
            List<MODEL.qgzx_zm_course> list = OperateContext.Current.BLLSession.Iqgzx_zm_courseBLL.GetListBy(x => x.Id != null);
            List<SelectListItem> selectList = new List<SelectListItem>();
            foreach (MODEL.qgzx_zm_course model in list)
            {
                selectList.Add(new SelectListItem() { Text = model.CourseName, Value = model.Id.ToString() });
            }
            ViewBag.courselist = selectList;

            ///*
            // 性别
            // */
            //List<SelectListItem> sexList = new List<SelectListItem>() { 
            //    new SelectListItem() { Text = "男", Value = "1"},
            //    new SelectListItem() { Text = "女", Value = "2"}
            //};
            //ViewBag.sexList = sexList;
            ///*
            // 黑名单
            // */
            //List<SelectListItem> IsBlacklist = new List<SelectListItem>() { 
            //    new SelectListItem() { Text = "是", Value = "1"},
            //    new SelectListItem() { Text = "否", Value = "2"}
            //};
            //ViewBag.IsBlacklist = IsBlacklist;

        }
        #endregion

    }
}
